# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=headscale
pkgver=0.22.3
pkgrel=10
pkgdesc="Open source, self-hosted implementation of the Tailscale control server"
url="https://github.com/juanfont/headscale"
arch="all !ppc64le !riscv64 !loongarch64" # go build fails
license="BSD-3-Clause"
pkgusers="headscale"
pkggroups="headscale"
makedepends="go"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-openrc
	$pkgname-zsh-completion
	"
install="$pkgname.pre-install"
source="https://github.com/juanfont/headscale/archive/v$pkgver/headscale-$pkgver.tar.gz
	headscale.initd
	"
options="net" # fetch dependencies

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare

	# move socket to a subdirectory to allow running as non-root
	sed -i 's|/var/run/headscale.sock|/run/headscale/headscale.sock|' config-example.yaml
}

build() {
	go build -ldflags "-X github.com/juanfont/headscale/cmd/headscale/cli.Version=v$pkgver" ./cmd/headscale
	mkdir completions
	./headscale completion bash > completions/bash
	./headscale completion fish > completions/fish
	./headscale completion zsh > completions/zsh
}

check() {
	# headscale runs integration tests if /.dockerenv exists, but those
	# tests fail in gitlab CI because they rely on a certain environment
	# (see test_integration target in headscale's Makefile). so, use -short
	# to disable integration tests when packaging.
	go test -short ./...
}

package() {
	install -Dm755 headscale "$pkgdir"/usr/bin/headscale

	install -Dm755 "$srcdir"/headscale.initd "$pkgdir"/etc/init.d/headscale
	install -Dm644 config-example.yaml "$pkgdir"/etc/headscale/config.yaml

	# completions
	install -Dm644 completions/bash "$pkgdir"/usr/share/bash-completion/completions/headscale
	install -Dm644 completions/fish "$pkgdir"/usr/share/fish/vendor_completions.d/headscale.fish
	install -Dm644 completions/zsh "$pkgdir"/usr/share/zsh/site-functions/_headscale
}

sha512sums="
c3ce7ae023dd0bd5c52e00ec2564b68eb87074a804a9982a3eb6f1e64335621a312cbb650d79571e89bef3f0d8e6d602b732156aa8851b67dfd455ac1156b311  headscale-0.22.3.tar.gz
0800829bfc087af283afc117406324a0129b30b587c8cc5df85e147ac09fc879d726fc2d0b62ed545fb0190ed887641f07256745da9dea56932dd2d90aa41625  headscale.initd
"
